/*!
 * \file WtBtRunner.cpp
 * \project	WonderTrader
 *
 * \author Wesley
 * \date 2020/03/30
 *
 * \brief
 */
#include "../WtBtCore/ExecMocker.h"
#include "../WtBtCore/HisDataReplayer.h"
#include "../WtBtCore/UftMocker.h"
#include "../WtBtCore/WtHelper.h"

#include "../WTSTools/WTSLogger.h"
#include "../WTSUtils/SignalHook.hpp"

#include "../Includes/WTSVariant.hpp"
#include "../Share/StdUtils.hpp"
#include "../WTSUtils/WTSCfgLoader.h"

#ifdef _MSC_VER
#include "../Common/mdump.h"
#endif

int main() {
#ifdef _MSC_VER
    CMiniDumper::Enable("WtBtRunner.exe", true, WtHelper::getCWD().c_str());
#endif

    std::string filename = "logcfgbt.json";
    if (!StdFile::exists(filename.c_str()))
        filename = "logcfgbt.yaml";

    WTSLogger::init(filename.c_str());

#if _WIN32
#pragma message("Signal hooks disabled in WIN32")
#else
#pragma message("Signal hooks enabled in UNIX")
    install_signal_hooks(
        [](const char* message) { WTSLogger::error(message); });
#endif

    filename = "configbt.json";
    if (!StdFile::exists(filename.c_str()))
        filename = "configbt.yaml";

    WTSVariant* cfg = WTSCfgLoader::load_from_file(filename.c_str());
    if (cfg == NULL) {
        WTSLogger::info("Loading configuration file {} failed", filename);
        return -1;
    }

    HisDataReplayer replayer;
    replayer.init(cfg->get("replayer"));

    WTSVariant* cfgEnv = cfg->get("env");
    const char* mode = cfgEnv->getCString("mocker");
    int32_t slippage = cfgEnv->getInt32("slippage");
    if (strcmp(mode, "cta") == 0) {
    }
    else if (strcmp(mode, "hft") == 0) {
    }
    else if (strcmp(mode, "sel") == 0) {
    }
    else if (strcmp(mode, "exec") == 0) {
        ExecMocker* mocker = new ExecMocker(&replayer);
        mocker->init(cfg->get("exec"));
        replayer.register_sink(mocker, "exec");
    }
    else if (strcmp(mode, "uft") == 0) {
        UftMocker* mocker = new UftMocker(&replayer, "uft");
        mocker->init_uft_factory(cfg->get("uft"));
        const char* stra_id =
            cfg->get("uft")->get("strategy")->getCString("id");
        replayer.register_sink(mocker, stra_id);
    }

    replayer.prepare();

    replayer.run(true);

    printf("press enter key to exit\r\n");
    getchar();

    WTSLogger::stop();
}
